﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IVR3Downloader.Class
{
    public class ProgressionDownload
    {
        private static string[] SIZES = { "B", "KB", "MB", "GB", "TB" };


        public ulong FileSize { get; set; }
        public ulong FileSizeDownloaded { get; set; }

        public float FileSizePercentage
        {
            get
            {
                if (this.FileSize == 0) return 0;

                return (float)(this.FileSizeDownloaded * 100) / (float)this.FileSize;
            }
        }

        public String UserFileSize
        {
            get
            {
                return ProgressionDownload.CalculateUserSize(this.FileSize);
            }
        }

        public String UserFileSizeDownloaded
        {
            get
            {
                return ProgressionDownload.CalculateUserSize(this.FileSizeDownloaded);
            }
        }

        public String DownloadError { get; set; }

        internal static String CalculateUserSize(ulong aValue)
        {
            int counter = 0;
            double result = aValue;

            do
            {
                if (result < 1024 || counter >= 4)
                {
                    break;
                }

                result = result / 1024;
                counter++;
            }
            while (true);

            return String.Format("{0:0.00} {1}", result, SIZES[counter]);
        }
    }
}
